<?php
class Plugins_LayoutSwitcher extends Zend_Controller_Plugin_Abstract
{

	/*
	 * This is quick plugin which will take care of dynamic layouts and will assing defualt values used in layouts and partials
	 */
	public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request){
		// Init view access
		$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
		$viewRenderer->initView();
		$view = $viewRenderer->view;
			
		// This is Assigning current route_id for dynamic page building

		$view->route_id = Zend_Controller_Front::getInstance()->getRouter()->getCurrentRouteName();
		
		$view->cms = $request->getParam('cms');

		$layout = Zend_Layout::getMvcInstance();

		// Thsi is switching ty layout based on the configurtion file.
		if($request->getParam("layout") != "")
		{

			$layout->setLayout($request->getParam("layout"));
		}

		if($request->isXmlHttpRequest() && ($request->getControllerName() != "ajax"))
		{
			$layout->disableLayout();
		}

		$view->placeholder("master")->view = $view;
	}
}
